<?php use_helper('Javascript') ?>
<script src="http://maps.google.com/maps?file=api&v=2.x&key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA" type="text/javascript"></script>
<script type="text/javascript" src="http://maps.gstatic.com/intl/lt_ALL/mapfiles/334b/maps2.api/main.js"></script>
<script type="text/javascript">
jQuery(window).load(function() {
    initialize();
});
jQuery(window).unload(function() {
    GUnload();
});
jQuery(document).keypress(function(event) {
    if (event.which == '13') {
        event.preventDefault();
        showAddress(jQuery('#calendar_address').val());
    }
});
function OnGoogleClicked(point, name)
{
//    alert(name);
    jQuery('#calendar_latitude').val(point.lat());
    jQuery('#calendar_longitude').val(point.lng());
    // To prevent on change event
    gGeoMask = name;
}
var map = null;
var geocoder = null;

function initialize() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(jQuery('#calendar_latitude').val(), jQuery('#calendar_longitude').val()), 13);
    geocoder = new GClientGeocoder();
  }
}

function showAddress(address) {
  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          alert(address + " not found");
        } else {
          OnGoogleClicked(point, address);
          map.setCenter(point, 13);
          var marker = new GMarker(point);
          map.addOverlay(marker);

          // As this is user-generated content, we display it as
          // text rather than HTML to reduce XSS vulnerabilities.
          marker.openInfoWindow(document.createTextNode(address));
        }
      }
    );
  }
}
</script>
<script type="text/javascript" charset="UTF-8" src="http://maps.gstatic.com/cat_js/intl/lt_ALL/mapfiles/334b/maps2.api/%7Bmod_drag,mod_ctrapi,mod_api_gc%7D.js">
</script>
<p>
    <?php //if calendar latitude not set, set vilnius latitude ?>
    <?php echo input_hidden_tag('calendar[latitude]'
                            , $calendar->getPointLatitude()?$calendar->getPointLatitude():54.6893865); ?>
    <?php //if calendar longitude not set, set vilnius longitude ?>
    <?php echo input_hidden_tag('calendar[longitude]', $calendar->getPointLongitude()?$calendar->getPointLongitude():25.2800243); ?>
    <?php echo input_tag('calendar[address]', $calendar->getAddress()?$calendar->getAddress():'Vilnius', array('size' => 60))?>
    <?php echo input_tag('go', __('Go!'), array("onClick" => "showAddress(jQuery('#calendar_address').val()); return false"
                                            , "type" => "button"))?>
</p>
<div id="map_canvas" style="width: 500px; height: 300px"></div>